package com.kxn.multi.module.api.controller;

import com.kxn.multi.module.common.exception.ServiceException;
import com.kxn.multi.module.common.pojo.ApiResult;
import com.kxn.multi.module.service.dto.api.user.UserLoginReq;
import com.kxn.multi.module.service.dto.api.user.UserRegisterDto;
import com.kxn.multi.module.service.dto.api.user.UserRegisterReq;
import com.kxn.multi.module.service.dto.api.user.UserLoginDto;
import com.kxn.multi.module.service.entity.User;
import com.kxn.multi.module.service.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import kotlin.DeprecatedSinceKotlin;
import org.apache.shardingsphere.infra.hint.HintManager;
import org.junit.Test;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = {"用户管理"})
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Resource
    UserService userService;

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public ApiResult register(@Validated  @RequestBody UserRegisterReq userRegisterReq) {
        UserRegisterDto userRegisterDto = userService.register(userRegisterReq);
        return ApiResult.success(userRegisterDto);
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public ApiResult login(@Validated  @RequestBody UserLoginReq userLoginReq) {
        UserLoginDto userLoginDto = userService.login(userLoginReq);
        return ApiResult.success(userLoginDto);
    }


    @ApiOperation("添加用户")
    @PostMapping("/create")
    public ApiResult insert(@RequestBody @Validated User user) {
        if (userService.getUserByUsername(user.getUsername()) != null || userService.getUserByPhone(user.getPhone()) != null) {
            throw new ServiceException("用户名或者手机号已存在");
        }
        userService.save(user);
        return ApiResult.success(user);
    }

    @ApiOperation("用户详情")
    @GetMapping("/master/{id}")
    public ApiResult getMasterDetail(@PathVariable Long id) {
        User userInfo = null;

        // 强制使用主库（写库）
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setWriteRouteOnly();
            userInfo = userService.getById(id);
        }
        return ApiResult.success(userInfo);
    }

    @ApiOperation("用户详情")
    @GetMapping("/{id}")
    public ApiResult detail(@PathVariable Long id) {
        User userInfo = userService.getById(id);
        return ApiResult.success(userInfo);
    }

    @ApiOperation("删除用户")
    @PostMapping("/delete/{id}")
    public ApiResult delete(@PathVariable Long id) {
        userService.removeById(id);
        return ApiResult.success();
    }

    @ApiOperation("更新用户")
    @PostMapping("/update/{id}")
    public ApiResult update(@PathVariable Long id, @RequestBody User user) {

        User oldUser = userService.getById(id);
        if (oldUser == null) {
            ApiResult.error("数据不存在");
        }
        user.setId(id);
        userService.updateById(user);
        return ApiResult.success(user);
    }


}
